package com.sharer.last.wrapper;

import com.sharer.last.base.support.BaseEntityWrapper;
import com.sharer.last.base.utils.CollectionUtil;
import com.sharer.last.model.po.BlogPO;
import com.sharer.last.model.po.BlogTagPO;
import com.sharer.last.model.po.CategoryPO;
import com.sharer.last.model.po.TagPO;
import com.sharer.last.model.vo.blog.BlogVO;
import com.sharer.last.service.BlogTagService;
import com.sharer.last.service.CategoryService;
import com.sharer.last.service.TagService;
import com.sharer.last.service.UserService;
import com.sharer.last.utils.BeanUtil;
import com.sharer.last.utils.CommonUtil;
import com.sharer.last.utils.markdown.MarkdownUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 博客转换器
 *
 * @ClassName BlogWrapper
 * @Author WangJin
 * @Date 2023/06/09/10:39
 * @Description
 * @Version 1.0
 */
@Component
public class BlogWrapper extends BaseEntityWrapper<BlogPO, BlogVO> {
    @Resource
    CategoryService categoryService;

    @Resource
    BlogTagService blogTagService;

    @Resource
    TagService tagService;

    @Resource
    UserService userService;

    /**
     * 单个实体类包装
     *
     * @param entity
     * @return
     */
    @Override
    public BlogVO entityVO(BlogPO entity) {
        BlogVO vo = BeanUtil.copy(entity, BlogVO.class);
        CategoryPO categoryPO = Optional.ofNullable(categoryService.getById(vo.getCategoryId())).orElse(new CategoryPO());
        vo.setCategoryName(categoryPO.getCategoryName());
        List<BlogTagPO> blogTagPOList = blogTagService.queryListByBlogId(vo.getId());
        List<Long> tagIdList = blogTagPOList.stream().map(BlogTagPO::getTagId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(tagIdList)) {
            List<TagPO> tagPOList = tagService.queryListByIds(tagIdList);
            vo.setTagList(tagPOList);
        }
        vo.setContent(MarkdownUtils.markdownToHtmlExtensions(vo.getContent()));
        vo.setDescription(MarkdownUtils.markdownToHtmlExtensions(vo.getDescription()));
        return vo;
    }

    public List<BlogVO> listVO(List<BlogPO> poList) {
        // 查询分类数据
        List<CategoryPO>      categoryPOList = categoryService.list();
        Map<Long, CategoryPO> categoryMap    = new HashMap<>();
        for (CategoryPO categoryPO : categoryPOList) {
            categoryMap.put(categoryPO.getId(), categoryPO);
        }
        // 查询文章标签
        List<TagPO>                tagPOList     = tagService.list();
        List<BlogTagPO>            blogTagPOList = blogTagService.list();
        Map<Long, List<BlogTagPO>> blogTagMap    = blogTagPOList.stream().collect(Collectors.groupingBy(BlogTagPO::getBlogId));
        Map<Long, List<TagPO>>     tagMap        = new HashMap<>();
        if (CollectionUtil.isNotEmpty(blogTagMap)) {
            blogTagMap.forEach((k, v) -> {
                List<TagPO> tagIdList     = v.stream().map(m -> TagPO.builder().id(m.getTagId()).build()).collect(Collectors.toList());
                List<TagPO> intersectList = CommonUtil.intersect(tagPOList, tagIdList);
                if (CollectionUtil.isNotEmpty(intersectList)) {
                    tagMap.put(k, intersectList);
                }
            });
        }
        return poList.stream().map(item -> {
            BlogVO vo = BeanUtil.copy(item, BlogVO.class);
            vo.setCategoryName(Optional.ofNullable(categoryMap.get(item.getCategoryId())).orElse(new CategoryPO()).getCategoryName());
            vo.setTagList(Optional.ofNullable(tagMap.get(item.getId())).orElse(new ArrayList<>()));
            vo.setContent(MarkdownUtils.markdownToHtmlExtensions(vo.getContent()));
            vo.setDescription(MarkdownUtils.markdownToHtmlExtensions(vo.getDescription()));
            return vo;
        }).collect(Collectors.toList());
    }
}
